const fs = require('fs');
const path = require('path');
let targetPath = path.resolve(__dirname, 'src/pages/global_monitor/canvas_manage/requireModels.js');
let filePath = '../../../../public/canvas-board';

fs.readdir(path.resolve(__dirname, 'public/canvas-board'), (err, result)=>{
    // 读取目录中所有模型图片文件
    if ( err ) return;
    let str='', pathArr = [];
    if ( result.length ) {
        // 生成引入资源的import语句;
        result.forEach(name=>{
            let temp = name.split('.');
            let fileName = temp.length > 2 ? temp[0] + ( temp[1] === 'a' ? 'On' : 'Off' ) : temp[0];
            pathArr.push(fileName);
            str += `import ${fileName} from '${filePath}/${name}';\r\n`;
        });
        str += 'let obj = {};\r\n';
        str += `(function(originArr, pathArr){
            originArr.forEach((name, index)=>{
                let temp = name.split('.');    
                if ( !obj[temp[0]] ) {
                    obj[temp[0]] = { path:null, onPath:null, offPath:null };
                }
                if ( temp.length > 2 ) {
                    if ( temp[1] === 'a' ) {
                        obj[temp[0]].onPath = pathArr[index];
                    } else {
                        obj[temp[0]].offPath = pathArr[index];
                    }
                } else {
                    obj[temp[0]].path = pathArr[index];
                }
            });
        })(${JSON.stringify(result)}, [${pathArr}]);\r\n`
        str += 'export let modelsPath = obj;\r\n';
        str += `export let modelsMap = {
            'coldMeter':{ title:'冷量计', width:378, height:103 },
            'eleMeter':{ title:'电表', width:67, height:80 },
            'flowMeter':{ title:'流量计', width:76, height:92 },
            'hostMach':{ title:'制冷主机', width:210, height:113 },
            'pressureMeter':{ title:'压力计', width:100, height:154 },
            'smartValve':{ title:'智能阀', width:720, height:720 },
            'valve':{ title:'阀', width:84, height:52 },
            'waterCollector':{ title:'集水器', width:192, height:69 },
            'waterDispatcher':{ title:'分水器', width:192, height:69 },
            'waterMeter':{ title:'水表', width:797, height:579 },
            'waterPump':{ title:'水泵', width:51, height:67 },
            'waterTower':{ title:'冷却塔', width:96, height:140 }
        };\r\n`;        
        // 生成图片资源和路径关系的映射对象
        
        fs.writeFile(targetPath, str, (err, res)=>{
            console.log(err);
            console.log(res);
        })
    }
})